package com.application.DataProvider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.application.DTO.SubjectDTO;

public class SubjectDAO{

	Context _mcontext = null;

	public SubjectDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		Log.d("PEERDROID","vao isExist subject");
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Subject where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","thoat isExist subject: " + result);
		return result;
	}
	
	//Lấy danh sách id subject, chapter, part, học trong ngày hôm nay
//	public String getLearning(int idClass)
//	{
//		DataProvider provider = new DataProvider(_mcontext);
//		SQLiteDatabase db = provider.openDataBase();
//		boolean result = false;
//		Cursor cur = null;
//		String s= null;
//		try 
//		{
//			String sql = "select c.idsubject, p.idchapter, dl.idpart from subject s, chapter c, part p, detaillearning dl"+
//						"where s.id = c.idsubject and c.id = p.idchapter and p.id = dl.idpart and dl.idclass = "+ idClass + 
//						" and datediff(day, dl.datelearning, getdate())=0 order by  c.idsubject, p.idchapter, dl.idpart";
//			cur = provider.execQuery(db, sql);
//			while (cur.moveToNext()) {
//				if(s != null)
//					s+=ReadWriteFileController1.getSeparator1();
//				s+= cur.getInt(0)+ ReadWriteFileController1.getSeparator() + cur.getInt(1) + ReadWriteFileController1.getSeparator() + cur.getInt(2); 
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		} finally{
//			cur.close();
//			db.close();
//		}
//		return s;
//	}
	
	public ArrayList<ArrayList<String>> getSubject_Class(int idClass){
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		ArrayList<ArrayList<String>> subList = new ArrayList<ArrayList<String>>();
		Log.d("PEERDROID", "vao getSubject_Class subject DAO ");
		Cursor cur = null;
		try 
		{
			String sql = "select t.fullname, s.Name, s.id from Subject s, Teacher t, JoinSubjectClass jsc where jsc.idclass = "+idClass+" and s.id = jsc.idsubject and t.id = s.idteacher";
			cur = provider.execQuery(db, sql);
			Log.d("PEERDROID", cur.toString());
			while (cur.moveToNext()) {
				ArrayList<String> kq = new ArrayList<String>();
				kq.add(cur.getString(0));
				kq.add(cur.getString(1));
				kq.add(cur.getString(2));
				subList.add(kq);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return subList;
	}
	public SubjectDTO getSubject(int idSubject){
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		SubjectDTO sDto = new SubjectDTO();
		Log.d("PEERDROID", "vao getSubject");
		Cursor cur = null;
		try 
		{
			String sql = "select * from Subject where id = " + idSubject;
			cur = provider.execQuery(db, sql);
			Log.d("PEERDROID", cur.toString());
			while (cur.moveToNext()) {
				sDto.set_ID(cur.getInt(0));
				sDto.set_Name(cur.getString(1));
				sDto.set_Note(cur.getString(2));
				sDto.set_IDTeacher(cur.getInt(3));
				sDto.set_Content(cur.getString(4));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return sDto;
	}
	public ArrayList<ArrayList<String>> getSubject_Teacher(int idTeacher){
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		ArrayList<ArrayList<String>> subList = new ArrayList<ArrayList<String>>();
		Log.d("PEERDROID", "bat dau get subject");
		Cursor cur = null;
		try 
		{
			String sql = "Select tc.FullName , sj.Name, sj.ID from Teacher tc, Subject sj where tc.ID =" + idTeacher + " and sj.idteacher = tc.id";
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ArrayList<String> kq = new ArrayList<String>();
				kq.add(cur.getString(0));
				kq.add(cur.getString(1));
				kq.add(cur.getString(2));
				subList.add(kq);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
			
		}

		return subList;
	}
	//Insert du lieu vao bang Subject
	public long insertSubject(SubjectDTO sj_dto) {
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", sj_dto.get_ID());
			values.put("Name", sj_dto.get_Name());
			values.put("IDTeacher", sj_dto.get_IDTeacher());
			values.put("Note", sj_dto.get_Note());
			values.put("Content", sj_dto.get_Content());
			values.put("Status",sj_dto.isNew());

			result = provider.execInsertCommand(db, "Subject", values);
			Log.d("PEERDROID","insert thanh cong");
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang Subject
	public int updateSubject(SubjectDTO sj_dto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("ID", sj_dto.get_ID());
			values.put("Name", sj_dto.get_Name());
			values.put("IDTeacher", sj_dto.get_IDTeacher());
			values.put("Note", sj_dto.get_Note());
			values.put("Content", sj_dto.get_Content());
			values.put("Status",sj_dto.isNew());
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(sj_dto.get_ID())};
			result= provider.execUpdateCommand(db, "Subject", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			db.close();
		}
		return result;
	}

	public int updateStatusSubject(boolean status, int idSub)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			
			values.put("Status",status);
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idSub)};
			result= provider.execUpdateCommand(db, "Subject", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			db.close();
		}
		return result;
	}
	public int getTeacher(int id)
	{
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select IDTeacher from Subject where ID = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				cur.getInt(0);
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result status : " + result);
		return result;
	}
	public boolean getStatus(int idSub)
	{
		boolean result = false;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Status from Subject where ID = " + idSub;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getString(0).equals("1"))
					result = true;
				
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result status : " + result);
		return result;
	}
	//Delete doi tuong SubjectDTO
	public int deleteSubject(int idSubject)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idSubject)};	
			result=provider.execDeleteCommand(db, "Subject", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			db.close();
		}
		return result;
	}

	//Delete bang Subject
	public int deleteSubject() {

		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			String sql = "delete from Subject";
			result=provider.execDeleteCommand(db, "Subject", null, null);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}
}
